const { systemPreferences ,desktopCapturer, app, BrowserWindow, ipcMain } = require("electron");
const { join } = require("path");
const { machineIdSync } = require("node-machine-id");
let win; // 用于登录窗口
let mainWindow; // 用于主窗口
let deviceId; //设备id

//创建第一个窗口登录窗口
const createLoginWindow = () => {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    title: "Login",
    icon: join(__dirname, "../public/desk_img.png"),
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: true,
    },
  });

  // win.loadURL("http://localhost:3000/#/login");
  // ipcMain.on('request-screen-permission', async () => {
  //   const screenPrivilege = await systemPreferences.getMediaAccessStatus('screen');
  //   if (screenPrivilege !== 'granted') {
  //     await systemPreferences.askForMediaAccess('screen');
  //   }
  // })



  win.on("closed", () => {
    win = null;
  });

  console.log("2222222222222222222222222222");
  console.log(process.env.VITE_DEV_SERVER_URL);
  console.log("2222222222222222222222222222");
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    win.webContents.openDevTools();
  } else {
    win.loadFile(join(__dirname, "../dist/index.html"));
     win.webContents.openDevTools(); //上线的需注释
  }
};

const createMainWindow = () => {
  mainWindow = new BrowserWindow({
    width: 1600,
    height: 986,
    title: "直播页",
    icon: join(__dirname, "../public/desk_img.png"),
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: true,
    },
  });
  // mainWindow.loadURL("http://localhost:3000/#/home");
  if (process.env.VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL + "#/home");
    mainWindow.webContents.openDevTools();
  } else {
    mainWindow.loadFile(join(__dirname, "../dist/index.html"), {
      hash: "home",
    });
    mainWindow.webContents.openDevTools();// 上线的需注释
  }

  mainWindow.on("closed", () => {
    mainWindow = null;
  });
  // 
  ipcMain.on("process-all",async (event) => {
   let resouresRes= await desktopCapturer.getSources({ types: ['window', 'screen'] })
    event.sender.send('resoures',resouresRes)
  });


  
};

// contextBridge.exposeInMainWorld('electron', {
//   allowRenderer: (url) => {
//     // 允许加载远程图片
//     return true;
//   }
// });
//进程（整个程序）加载完成
app.whenReady().then(() => {
  deviceId = machineIdSync();
  console.log("deviceId");
  console.log(deviceId);
  console.log("deviceId");
  createLoginWindow();
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

app.on("activate", () => {
  if (!win && !mainWindow) {
    createLoginWindow();
  }
});

//事件部分
//成功
ipcMain.on("login-success", () => {
  if (win) {
    win.close(); // 关闭登录窗口
  }
  createMainWindow(); // 创建主窗口
});

//失败
ipcMain.on("login-out", () => {
  if (mainWindow) {
    mainWindow.close(); // 关闭登录窗口
  }
  createLoginWindow(); // 创建主窗口
});

ipcMain.on("windows_close", () => {
  app.quit()
});
ipcMain.on("windows_fullscreen", () => {
  mainWindow.setFullScreen(true)
});
ipcMain.on("windows_setmaximize", () => {
  mainWindow.setFullScreen(false)
});
ipcMain.on("windows_minimize", () => {
  mainWindow.minimize()
});
ipcMain.on("login_minimize", () => {
  win.minimize()
});

ipcMain.on("getDeviceId", (event) => {
  event.sender.send("returnDeviceId", deviceId);
});
